<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-eightapp__eihouseinfo}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('eightapp:eihouseinfo:save')" type="primary" @click="addOrUpdateHandle()">
            {{ $t('add') }}
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('eightapp:eihouseinfo:delete')" type="danger" @click="deleteHandle()">
            {{ $t('deleteBatch') }}
          </el-button>
        </el-form-item>
      </el-form>
      <div class="tableC" style="position: relative; ">
        <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle"
                  style="width: 100%;"
                  :height="tableMaxHeight">
          <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="id" label="房屋编号" header-align="center" align="center"></el-table-column>
          <el-table-column prop="type" label="房产类型" header-align="center" align="center"></el-table-column>
          <el-table-column prop="area" label="房屋面积" header-align="center" align="center"></el-table-column>
          <el-table-column prop="houseTime" label="生效日期" header-align="center" align="center"></el-table-column>
          <el-table-column prop="unit" label="单元号" header-align="center" align="center"></el-table-column>
          <el-table-column prop="creator" label="创建人" header-align="center" align="center"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" header-align="center" align="center"
                           width="100px"></el-table-column>
          <el-table-column prop="updator" label="更新者" header-align="center" align="center"></el-table-column>
          <el-table-column prop="updateTime" label="更新时间" header-align="center" align="center"
                           width="100px"></el-table-column>
          <el-table-column prop="icon" label="房屋照片" header-align="center" align="center" width="300px">
            <template slot-scope="scope">
              <!-- {{ scope.row }} -->
              <!--              \files\park\0e6d24a4-3226-4101-92fa-00daf2c1c0e0.jpg-->
              <!--            /file/img?name=2022/12/29/a6e64d67-aa08-46ca-a954-6d106caac1b8.jpg-->
              <el-image style="width: 200px; height: 200px" :src="scope.row.icon">
              </el-image>
              <el-divider></el-divider>
              <el-upload
                  class="upload-demo"
                  action="/file/upload"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  :on-preview="handlePreview"
                  list-type="picture"
                  :file-list="fileList"
                  :show-file-list="false"
              >
                <el-button size="small" type="primary" @click="getRowId(scope.row.id)">点击上传</el-button>
              </el-upload>
            </template>
          </el-table-column>
          <el-table-column prop="buildingId" label="楼房编号" header-align="center" align="center"></el-table-column>
          <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
            <template slot-scope="scope">
              <el-button v-if="$hasPermission('eightapp:eihouseinfo:update')" type="text" size="small"
                         @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}
              </el-button>
              <el-button v-if="$hasPermission('eightapp:eihouseinfo:delete')" type="text" size="small"
                         @click="deleteHandle(scope.row.id)">{{ $t('delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './eihouseinfo-add-or-update'

export default {
  mixins: [mixinViewModule],
  data() {
    return {
      fileList: [],
      fileInfo: '',
      mixinViewModuleOptions: {
        getDataListURL: '/eightapp/eihouseinfo/page',
        getDataListIsPage: true,
        exportURL: '/eightapp/eihouseinfo/export',
        deleteURL: '/eightapp/eihouseinfo',
        deleteIsBatch: true
      },
      dataForm: {
        id: ''
      },
      house: {
        id: '',
        icon: ''
      }
    }
  },
  methods: {
    //上传图片成功之后要做的工作
    handleAvatarSuccess(res) {
      if (res.code == 0) {
        this.house.icon = res.data
        //查询原先数据库中的icon
        this.getPicsql()

        //更新储存数据库
        this.setPicsql();
        //刷新
        this.$notify({
          title: '上传成功',
          type: 'success'
        })
      } else {
        this.$notify({
          title: '上传失败',
          type: 'error'
        })
      }
    },
    getPicsql() {
      this.$http.get(`/eightapp/eihouseinfo/${this.house.id}`).then(({data: res}) => {
        this.fileInfo = res.data
        console.log(res.data)
        //删除原来icon对应的照片
        this.handleRemove(res.data.icon)
      }).catch(() => {
      })
    },
    setPicsql() {
      this.$http.put(`/eightapp/eihouseinfo/setHouseIcon`, this.house).then(({data: res}) => {
        this.query();
      }).catch(() => {
      })
    },
    getRowId(id) {
      this.house.id = id
    },
    //上传图片之前的校验
    beforeAvatarUpload(file) {
      const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png');
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!');
        return false;
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
        return false;
      }
      return true;
    },
    handleRemove(file) {
     let newfile= String(file).substring(20)
      console.log(newfile)
      this.$http
          .delete(`/file/${newfile}`).then(({data: res}) => {
            this.query()
        this.$message({
          message: `${res.data}`,
          type: "success",
        });
      });
    },
    handlePreview(file) {
      console.log(file);
    },
  },
  computed: {
    tableMaxHeight() {
      return window.innerHeight - 170 + 'px';
    }
  },
  components: {
    AddOrUpdate
  }
}
</script>
<style>
.tableC {
  position: relative;
}

.el-table th {
  height: 60px;
}

.el-table td {
  padding: 0;
  height: 50px;
  font-size: 14px;
}

.el-table.hide-scrollBar {
  /*隐藏滚动条 设置底部padding20px*/
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE 10+ */
  padding-bottom: 20px;
}

.el-table.hide-scrollBar ::-webkit-scrollbar {
  display: none; /* Chrome Safari */
}
</style>